/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package common.xmlObjects;

import common.Objects.Component;
import common.Objects.Product;
import java.util.Vector;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Richard Irons
 */
public class ProductXML extends XMLNode {
    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String DESCRIPTION = "description";
    public static final String COMPONENT = BaseConnectionXML.COMPONENT;
    public static final String PRICE = "price";

    private XMLNode id;
    private XMLNode name;
    private XMLNode description;
    private Vector<ComponentXML> components;
    private XMLNode price;

    public ProductXML() {
        this(null,null,null,null,null);
    }
    public ProductXML(Product product) {
        this(
            String.valueOf(product.getId()),
            product.getName(),
            product.getDescription(),
            product.getComponents(),
            String.valueOf(product.getPrice()));
    }
    public ProductXML (
            String id,
            String name,
            String description,
            Vector<Component> components,
            String price) {

        super(BaseConnectionXML.PRODUCT,null);
        initChildren();
        
        setIdData(id);
        setNameData(name);
        setDescriptionData(description);
        createComponents(components);
        setPriceData(price);

        reAttachChildren();
    }
    public ProductXML (String xml) {
        this(BaseConnectionXML
            .XMLStrTODOMDoc(xml)
            .getElementsByTagName(
                BaseConnectionXML.PRODUCT)
                .item(0));
    }
    public ProductXML (Node node) {
        this ();
        NodeList nodeList = node.getChildNodes();

        for (int j = 0; j < nodeList.getLength(); j++) {
            Element element = (Element) nodeList.item(j);
            String elementName = element.getTagName();
            String elementValue = element.getTextContent();
            if (elementName.equals(ComponentXML.ID)) {
                this.setIdData(elementValue);
            }
            else if (elementName.equals(ProductXML.NAME)) {
                this.setNameData(elementValue);
            }
            else if (elementName.equals(ProductXML.DESCRIPTION)) {
                this.setDescriptionData(elementValue);
            }
            else if (elementName.equals(ProductXML.COMPONENT)) {
                this.addComponent(new ComponentXML((Node)element).getObject());
            }
            else if (elementName.equals(ProductXML.PRICE)) {
                this.setPriceData(elementValue);
            }
            reAttachChildren();
        }
    }
    private void createComponents(Vector<Component> components) {
        if (components != null) {
            for (Component component : components) {
                this.components.add(new ComponentXML(component));
            }
        }
    }
    private void attachComponents() {
        for (ComponentXML component : components) {
            addChildNode(component);
        }
    }
    private void initChildren(){
        id = new XMLChildNode(ID,null);
        name = new XMLChildNode(NAME,null);
        description = new XMLChildNode(DESCRIPTION,null);
        components = new Vector<ComponentXML>();
        price = new XMLChildNode(PRICE,null);
    }
    public void reAttachChildren() {
        this.removeAllChildNodes();
        addChildNode(id);
        addChildNode(name);
        addChildNode(description);
        attachComponents();
        addChildNode(price);
        optimiseChildren();
    }
    public void optimiseChildren() {
        if (id.getNodeData() == null || id.getNodeData().isEmpty() || id.getNodeData().equals("-1")) {
            id.setNodeData("-1");
            removeChildNode(id);
        }
        if (name.getNodeData() == null || name.getNodeData().isEmpty()) {
            name.setNodeData("");
            removeChildNode(name);
        }
        if (description.getNodeData() == null || description.getNodeData().isEmpty()) {
            description.setNodeData("");
            removeChildNode(description);
        }
        if (price.getNodeData() == null || price.getNodeData().isEmpty() ||price.getNodeData().equals("-1")) {
            price.setNodeData("-1");
            removeChildNode(price);
        }
    }
    public Product getObject() {
        Product product = new Product();
        product.setId(Integer.parseInt(getIdData()));
        product.setName(getNameData());
        product.setDescription(getDescriptionData());
        product.setComponents(getComponents());
        product.setPrice(Double.parseDouble(getPriceData()));
        return product;
    }
    public Vector<Component> getComponents() {
        Vector<Component> componentObjects = new Vector<Component>();
        for (ComponentXML component : this.components) {
            componentObjects.add(component.getObject());
        }
        return componentObjects;
    }
    public void addComponent(Component component) {
        if (component == null) {
            component = new Component();
        }
        this.components.add(new ComponentXML(component));
        attachComponents();
        reAttachChildren();
    }
    public void setComponents(Vector<Component> components) {
        if (components == null) {
            components = new Vector<Component>();
        }
        createComponents(components);
        reAttachChildren();
    }

    public String getDescriptionData() {
        return description.getNodeData();
    }

    public void setDescriptionData(String description) {
        if (description == null || description.isEmpty()) {
            description = "";
        }
        this.description.setNodeData(description);
        reAttachChildren();
    }

    public String getIdData() {
        return id.getNodeData();
    }

    public void setIdData(String id) {
        if (id == null || id.isEmpty()) {
            id = "-1";
        }
        this.id.setNodeData(id);
        reAttachChildren();
    }

    public String getNameData() {
        return name.getNodeData();
    }

    public void setNameData(String name) {
        if (name == null || name.isEmpty()) {
            name = "";
        }
        this.name.setNodeData(name);
        reAttachChildren();
    }

    public String getPriceData() {
        return price.getNodeData();
    }

    public void setPriceData(String price) {
        if (price == null || price.isEmpty()) {
            price = "-1";
        }
        this.price.setNodeData(price);
        reAttachChildren();
    }
}